#!-*- encoding:utf8 -*-
from django.template import Template, Context, RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect, HttpResponse
from newosmp.strategy.models import Strategy, Tpl
from newosmp.define import *
from newosmp.util import commom
from newosmp import settings

def index(request):
    page = int(request.GET.get('page', '1'))
    strategy = Strategy.objects.all()
    num = settings.PAGE
    count = strategy.count()
    (strategy, page_list, page_num) = commom.pagination(strategy, page, num)
    (page_up, page_down) = commom.getPage(page, page_num) #上下页
        #for s in strategy:
    #    s.tpl = Tpl.objects.get(id=s.tpl).name
    return render_to_response('strategy.html', locals())

def add(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        tpl = request.POST.get('tpl')
        if tpl == "host":
            lst ={}
            cpu_min = request.POST.get('cpu_min', -1)
            cpu_max = request.POST.get('cpu_max', -1)
            mem_min = request.POST.get('mem_min', -1)
            mem_max = request.POST.get('mem_max', -1)
            disk_min = request.POST.get('disk_min', -1)
            disk_max = request.POST.get('disk_max', -1)
            load_min = request.POST.get('load_min', -1)
            load_max = request.POST.get('load_max', -1)
            network_min = request.POST.get('cpu_min', -1)
            network_max = request.POST.get('cpu_max', -1)
            lst['cpu'] = "%s|%s" % (cpu_min, cpu_max)
            lst['mem'] = "%s|%s" % (mem_min, mem_max)
            lst['load'] = "%s|%s" % (load_min, load_max)
            lst['disk'] = "%s|%s" % (disk_min, disk_max)
            lst['network'] = "%s|%s" % (network_min, network_max)
            value = ",".join([ "%s:%s" %(k,v) for k, v in lst.items()])

        else:
            com_min = request.POST.get('com_min')
            com_max = request.POST.get('com_max')
            value = "value:%s|%s" % (com_min, com_max)

        
        strategy = Strategy.objects.create(name=name,
                    tpl=tpl,value=value,
                    detail=request.POST.get('detail'))
        strategy.save()
        id = strategy.id
        return HttpResponseRedirect('/strategy')
    else:
        tpl = TPL_ITEMS.keys()
        #无提交，转入模版，初始化数据
        return render_to_response('strategyadd.html', locals())


def delete(request):
    id = request.GET.get('id', '')
    if id:
        print id
        strategy = Strategy.objects.get(id = id)
        strategy.delete()
    return HttpResponseRedirect('/strategy')


def edit(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        name = request.POST.get('name')
        tpl = request.POST.get('tpl')
        if tpl == "host":
            lst ={}
            cpu_min = request.POST.get('cpu_min', -1)
            cpu_max = request.POST.get('cpu_max', -1)
            mem_min = request.POST.get('mem_min', -1)
            mem_max = request.POST.get('mem_max', -1)
            disk_min = request.POST.get('disk_min', -1)
            disk_max = request.POST.get('disk_max', -1)
            load_min = request.POST.get('load_min', -1)
            load_max = request.POST.get('load_max', -1)
            network_min = request.POST.get('cpu_min', -1)
            network_max = request.POST.get('cpu_max', -1)
            lst['cpu'] = "%s|%s" % (cpu_min, cpu_max)
            lst['mem'] = "%s|%s" % (mem_min, mem_max)
            lst['load'] = "%s|%s" % (load_min, load_max)
            lst['disk'] = "%s|%s" % (disk_min, disk_max)
            lst['network'] = "%s|%s" % (network_min, network_max)
            value = ",".join([ "%s:%s" %(k,v) for k, v in lst.items()])
        else:
            com_min = request.POST.get('com_min')
            com_max = request.POST.get('com_max')
            value = "value:%s|%s" % (com_min, com_max)

        strategy = Strategy.objects.get(id = id)
        strategy.name = name
        strategy.tpl = tpl
        strategy.value = value
        strategy.detail = request.POST.get('detail')
        strategy.save()
        return HttpResponseRedirect('/strategy')

    else:
        id = request.GET.get('id', '')
        tpl = TPL_ITEMS.keys()
        strategy = Strategy.objects.get(id = id)
        #strategy.tpl = Tpl.objects.get(id=strategy.tpl).name
    return render_to_response('strategyedit.html', locals())


def ajax_get_tpl(request):
    tpl = request.GET.get('tpl')
    id = request.GET.get('id', False)
    if id:
        strategy = Strategy.objects.get(id = int(id))
        if tpl == strategy.tpl:
            value = strategy.value
            if tpl == "host":
                dct = commom.str2dct(value)
                cpu = dct.get('cpu', "-1|-1").split('|')
                mem = dct.get('mem', "-1|-1").split('|')
                disk = dct.get('disk', "-1|-1").split('|')
                net = dct.get('net', "-1|-1").split('|')
                load = dct.get('load', "-1|-1").split('|')
                print cpu
            else:
                (min, max) = value.split(":")[1].split("|")

    if tpl == "host":
        return render_to_response('strategy-tpl-host.html', locals())
    else:
        return render_to_response('strategy-tpl-com.html', locals())
